from DrissionPage import ChromiumPage,ChromiumOptions
import re
import json
import os
import sys
import operator
import requests
import time
co = ChromiumOptions()
# 设置不加载图片、静音
co.no_imgs(True).mute(True)
url = "https://www.chrqj.com/vod/play/60021/sid/28045"
url = sys.argv[1]
saveDir = sys.argv[2]
page = ChromiumPage(co)
page.listen.start("m3u8")  # 开始监听，指定获取包含该文本的数据包
# 跳转到登录页面
page.get(url)
m3u8Url = ""
for packet in page.listen.steps():
    m3u8Url = packet.url
    if(operator.contains(m3u8Url,".js") == False and operator.contains(m3u8Url,".ts") == False
            and operator.contains(m3u8Url, ".css") == False  and operator.contains(m3u8Url,".jpg") == False
            and operator.contains(m3u8Url, ".htm") == False  and operator.contains(m3u8Url,".webmanifest") == False
            and operator.contains(m3u8Url, ".png") == False  and operator.contains(m3u8Url,".webp") == False
            and operator.contains(m3u8Url, "x-font-woff2") == False  and operator.contains(m3u8Url,".otf") == False):
        #print("m\t" + m3u8Url)
        page.stop_loading();
        break;
    #break;
baseUrl = "https://www.chrqj.com"
#print("m3u8Url:\t" + m3u8Url)
title = page.ele("@class=title_name")
title = title.text

#print("标题:" + title.text)
lists = page.eles(".:listitem")
pattern = r"<.*href='(.*)'>"
urlkv = {}
#data["m3u8Url"] = m3u8Url
#data["url"] = url
kvs = []
index = 1
sumCount = ""
saveDir = saveDir + "\\" +title + "\\"
for el in lists:
    ael = el.child()
    # <ChromiumElement a href='/vod/play/131215/sid/1128764'>
    a = re.match(pattern,str(ael))

    indexx = str(index)
    if (len(indexx) == 1):
        indexx = "0" + indexx
    # print("[" + indexx + "]" + baseUrl+a.group(1))
    filePath = saveDir + indexx + ".mp4"
    if os.path.exists(filePath) == False:
        urlkv[indexx] = baseUrl+a.group(1)
    sumCount = indexx
    index = index + 1
index = 1
for _key in urlkv.keys():
    sec = False
    while(sec == False):
        page1 = ChromiumPage(co)
        page1.listen.start("m3u8")  # 开始监听，指定获取包含该文本的数据包
        # 跳转到登录页面

        page1.get(urlkv[_key])
        m3u8Url = ""
        for packet in page1.listen.steps():
            m3u8Url = packet.url
            # print("m3u8Url : " + m3u8Url)
            if (operator.contains(m3u8Url, ".js") == False and operator.contains(m3u8Url, ".ts") == False
                    and operator.contains(m3u8Url, ".css") == False and operator.contains(m3u8Url, ".jpg") == False
                    and operator.contains(m3u8Url, ".htm") == False and operator.contains(m3u8Url, ".webmanifest") == False
                    and operator.contains(m3u8Url, ".png") == False and operator.contains(m3u8Url, ".webp") == False
                    and operator.contains(m3u8Url, "ad_m3u8") == False
                    and operator.contains(m3u8Url, "x-font-woff2") == False and operator.contains(m3u8Url,".otf") == False):
                kv = {}
                try:
                    kv["url"] = m3u8Url
                    if(len(urlkv[_key]) == 1):
                        kv["name"] = title
                    else:
                        kv["name"] = title + "/" + _key
                    index = index + 1
                    #kvs.append(kv)
                    kvs.append(kv)
                    # print(_url + "\t" + m3u8Url)
                    page.stop_loading();
                    sec = True
                except Exception as e:
                    # print("except " + _url)
                    time.sleep(5)
            break;
json_data = json.dumps(kvs)
print(json_data)
